<?php
////////////////////////////////////////////////////////////////////////////////
//	  This file is part of PushLMS.
//
//    PushLMS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    PushLMS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with PushLMS.  If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////

class QuestionsController extends AppController {

	var $scaffold = 'admin';

	var $helpers = array('Form', 'Javascript', 'Ajax');

	var $uses = array('Question', 'Answer', 'Answertype', 'Answervalue', 'Formtype', 'Questiongroup', 'Assessment', 'Asession', 'Asessionlog', 'Asessionresult');

	function beforeFilter() {

	}

	################
	# C.R.U.D METHODS
	################

	function index() {
		
		$this->set("CSS", "main");
		$questions = $this->Question->find('all');
		$this->set('questions', $questions);

	}
	
	function search(){

		$questions = $this->Question->find('all');
		$this->set('questions', $questions);

	}

	function order($id = null){

		if (!empty($this->data)) {
			
			$rank_count = 1;
			
			foreach ($this->data['Question']['answer_id'] as $key => $answer_id){
				$this->Answer->id = $answer_id;
				$this->Answer->saveField('rank', $rank_count);
				$rank_count ++;
			}

			$this->redirect('/questions/edit/'.$this->data['Question']['id']);
		
		}

		$question = $this->Question->findById($id);
		$this->set('question', $question);
		$this->data = $question;

	}

	function preview($id = null){

		if ($this->Session->read('Auth.User.usergroup_id') <= 2){

			$this->layout = 'notepad';
			$question = $this->Question->find('first',
				array(
					'conditions' => array('Question.id' => $id),
					'recursive' => 2, 
				)
			);

			$this->set('question', $question);
			$this->set('token', 'blank');
			$questiongroup = $this->Questiongroup->findById($question['Question']['questiongroup_id']);
			$this->set('questiongroup', $questiongroup);

		}else{

			$this->Session->setFlash("You do not have access to the question preview");
			$this->redirect('/users/');		

		}

	}

	################
	# POST UPDATE METHODS
	################

	public function reorder(){

		##error_log(print_r($this->data['questions'][0],true));

		$rank = 1;
		foreach ($this->data['questions'][0] as $question_id){
			
			if (preg_match('/^[0-9]+$/',$question_id))
			{
				$this->Question->id = $question_id;
				$this->Question->saveField('rank', $rank);
				$rank ++;
			}

		}

		$this->layout = 'none';

	}

	################
	# AJAX METHODS
	################

	public function ajax_get($id=null){

		if ($this->RequestHandler->isAjax()){

			Configure::write('debug', 0);

			if ($id != null){

				$data = $this->Questiongroup->findById($id);
				$assessment = $this->Assessment->findById($data['Questiongroup']['assessment_id']);
				$this->set('questiongroup_id', $data['Questiongroup']['id']);
				$this->set('data', $data);
				$this->set('currentquestion', $data);
				$this->set('assessment', $assessment);
				$this->layout = 'ajax';
				$this->render('ajax_get', 'ajax');
				$this->Session->setFlash("");

			}
	
		}

	}

	public function ajax_delete($id=null){

		if ($this->RequestHandler->isAjax()){

			Configure::write('debug', 0);

			if ($id != null){

				$question = $this->Question->findById($id);
				$this->Question->delete($id, true);
				$this->ajax_get($question['Question']['questiongroup_id']);

			}
	
		}

	}

	function ajax_edit($id = null) {

		if ($this->RequestHandler->isAjax()){

			if (!empty($this->data)) {

				Configure::write('debug', 0);

				if ($this->Question->save($this->data)){
					
					$question = $this->Question->findById($this->Question->id);
					$this->Session->setFlash("Question Data Updated!");
					$this->set('flash', true);
					$id = $this->Question->id;
					//$this->redirect('/questions/ajax_get/'.$question['Question']['questiongroup_id']);

				}
				else
				{
					$id = $this->data['Question']['id'];
				}

			}

			$answertypes = $this->Answertype->find('list');
			$this->set('answertypes', $answertypes);

			$answervalues = $this->Answervalue->find('list');
			$this->set('answervalues', $answervalues);

			$question = $this->Question->findById($id);
			$questiongroup = $this->Questiongroup->findById($question['Question']['questiongroup_id']);
			$assessment = $this->Assessment->findById($questiongroup['Questiongroup']['assessment_id']);

			$this->set('id',$id);
			$this->set('assessment', $assessment);
			$this->set('questiongroup', $questiongroup);
			$this->set('currentquestion', $question);

			$this->data = $question;

		}

	}

	public function ajax_add($questiongroup_id=null){

		if ($this->RequestHandler->isAjax()){

			Configure::write('debug', 0);

			if ($questiongroup_id != null){

				#error_log('assesmentid:'.$questiongroup_id);
	
				$newQuestion = array(
					'Question' => array(
						'name' => 'new-question',
						'question' => 'new-question',
						'answervalue_id'=> 1,
						'questiongroup_id'=> $questiongroup_id
					),
				);

				$this->Question->saveAll($newQuestion);

				#error_log('new question id:'.$this->Question->id);

				$question = $this->Question->findById($this->Question->id);
				$this->ajax_get($questiongroup_id);

			}
	
		}

	}

	public function ajax_duplicate($id=null, $questiongroup_id=false){

		if ($this->RequestHandler->isAjax()){

			Configure::write('debug', 0);

			if (!empty($id)) {

				$question_old = $this->Question->findById($id);
				$this->Question->copy($id);
				$this->Question->saveField('name',$question_old['Question']['name'].'--copy');

				if ($questiongroup_id){
					#error_log('question_id:'.$questiongroup_id);
					$this->ajax_get($questiongroup_id);
				}

			}

		}

	}

}

?>
